package org.linlinjava.litemall.admin.service.order_manage;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qcloud.cos.utils.Md5Utils;
import com.qiniu.util.Md5;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.linlinjava.litemall.admin.form.*;
import org.linlinjava.litemall.admin.util.ShortCodeUtil;
import org.linlinjava.litemall.admin.vo.order_manage.HomeAddAdVo;
import org.linlinjava.litemall.admin.vo.order_manage.HomeAddRecommendVo;
import org.linlinjava.litemall.admin.vo.order_manage.HomeAddSalePointVo;
import org.linlinjava.litemall.admin.vo.order_manage.HomeAddTopicVo;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.config.ServerAddress;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.dao.app.AppHomeMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.vo.app.HomeGoodsListVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AgedmallShopWebPageService {

    private Logger logger = LoggerFactory.getLogger(AgedmallShopStoreService.class);
    @Resource
    private AgedmallShopWebPageMapper agedmallShopWebPageMapper;
    @Resource
    private ServerAddress serverAddress;

    public Object list(WebPageListForm webPageListForm){
        AgedmallShopWebPageExample example = new AgedmallShopWebPageExample();
        AgedmallShopWebPageExample.Criteria criteria = example.createCriteria();

        if (webPageListForm.getShopId() != null) {
            criteria.andShopIdEqualTo(webPageListForm.getShopId());
        }

        criteria.andDeletedEqualTo(false);

        example.setOrderByClause("id desc");

        PageHelper.startPage(webPageListForm.getPage(), webPageListForm.getLimit());

        List<AgedmallShopWebPage> list = agedmallShopWebPageMapper.selectByExampleWithBLOBs(example);

        return ResponseUtil.okList(AgedmallShopWebPageSimple.parseList(list,serverAddress.getAdminHtml()));
    }

    public Object add(WebPageAddForm webPageAddForm){
        AgedmallShopWebPage shopWebPage = new AgedmallShopWebPage();
        shopWebPage.setShopId(webPageAddForm.getShopId());
        shopWebPage.setTitle(webPageAddForm.getTitle());
        shopWebPage.setHtml(webPageAddForm.getHtml());
        shopWebPage.setRemark(webPageAddForm.getRemark());
        shopWebPage.setSeoDescription(webPageAddForm.getSeoDescription());
        shopWebPage.setSeoKeywords(webPageAddForm.getSeoKeywords());
        shopWebPage.setKey(Md5.md5(ShortCodeUtil.generateCode(16).getBytes()));
        shopWebPage.setAddTime(LocalDateTime.now());
        shopWebPage.setUpdateTime(LocalDateTime.now());
        shopWebPage.setDeleted(false);
        agedmallShopWebPageMapper.insert(shopWebPage);
        return ResponseUtil.ok();
    }

    public Object edit(WebPageEditForm webPageEditForm){
        AgedmallShopWebPage shopWebPage = agedmallShopWebPageMapper.selectByPrimaryKey(webPageEditForm.getId());
        if (shopWebPage == null) {
            return ResponseUtil.fail("无效的网页ID");
        }
        shopWebPage.setTitle(webPageEditForm.getTitle());
        shopWebPage.setHtml(webPageEditForm.getHtml());
        shopWebPage.setRemark(webPageEditForm.getRemark());
        shopWebPage.setSeoDescription(webPageEditForm.getSeoDescription());
        shopWebPage.setSeoKeywords(webPageEditForm.getSeoKeywords());
        shopWebPage.setUpdateTime(LocalDateTime.now());
        agedmallShopWebPageMapper.updateByPrimaryKeySelective(shopWebPage);
        return ResponseUtil.ok();
    }

    public Object delete(WebPageDeleteForm webPageDeleteForm){
        AgedmallShopWebPage shopWebPage = agedmallShopWebPageMapper.selectByPrimaryKey(webPageDeleteForm.getId());
        if (shopWebPage == null) {
            return ResponseUtil.fail("无效的网页ID");
        }
        shopWebPage.setDeleted(true);
        agedmallShopWebPageMapper.updateByPrimaryKeySelective(shopWebPage);
        return ResponseUtil.ok();
    }

    public Object detail(WebPageDetailForm webPageDetailForm){
        AgedmallShopWebPage shopWebPage = agedmallShopWebPageMapper.selectByPrimaryKey(webPageDetailForm.getId());
        if (shopWebPage == null) {
            return ResponseUtil.fail("无效的网页ID");
        }
        return ResponseUtil.ok(AgedmallShopWebPageDetail.parseObject(shopWebPage,serverAddress.getAdminHtml()));
    }

    public AgedmallShopWebPageDetail view(String pid, String key) {
        AgedmallShopWebPageExample example = new AgedmallShopWebPageExample();
        AgedmallShopWebPageExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(Integer.parseInt(pid));
        criteria.andKeyEqualTo(key);
        criteria.andDeletedEqualTo(false);

        AgedmallShopWebPage shopWebPage = agedmallShopWebPageMapper.selectOneByExampleWithBLOBs(example);
        if (shopWebPage == null) {
            return null;
        }
        return AgedmallShopWebPageDetail.parseObject(shopWebPage,serverAddress.getAdminHtml());
    }
}
